/**
 * 题目链接：2025/10/28 考试 T1 整数二元组
 * 完成时间：2025/10/28 14:40
 * 简要思路：
 */
#include <bits/stdc++.h>
#define range(i, b, e) for (auto i(b); i != (e); i++)
#define rep(i, b, e) for (auto i(b); i <= (e); i++)
#define req(i, b, e) for (auto i(b); i >= (e); i--)
using namespace std;
typedef long long LL;
typedef pair<int, int> Pii;

const int N = 2e5;
int n, ans;
int fa[N*2 + 5], siz[N*2 + 5];
bool fl[N*2 + 5];

int find(int u) {
	return fa[u] == u ? u : fa[u] = find(fa[u]);
}

void merge(int u, int v) {
	u = find(u), v = find(v);
	if (u == v) fl[v] = true;
	else {
		fa[u] = v;
		siz[v] += siz[u];
		fl[v] |= fl[u];
	}
}

int main() {
	freopen("pairint.in", "r", stdin);
	freopen("pairint.out", "w", stdout);
	ios::sync_with_stdio(false);
	cin.tie(nullptr); cout.tie(nullptr);
	cin >> n;
	rep(i, 1, n*2) fa[i] = i, siz[i] = 1;
	for (int i = 1, u, v; i <= n; i++) {
		cin >> u >> v;
		merge(u, v);
	}
	rep(i, 1, n*2) {
		if (find(i) == i) ans += siz[find(i)] - !fl[find(i)];
	}
	cout << ans;
	return 0;
}
